﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Xsl;
using System.Reflection;

namespace GIGen
{
    public class ParseRequest
    {
        public string FilePath { get; set; }
        public string LanguageCode { get; set; }
        public string OutputFilePath { get; set; }
        public string MachineName { get; set; }

        private string _folderPath = "";


        public ParseRequest()
        {
            string sLocation = Assembly.GetExecutingAssembly().Location;
            _folderPath = sLocation.Substring(0, sLocation.LastIndexOf(@"\") + 1); // '\' is included
            this.FilePath = "";
            this.LanguageCode = "javascript";
            this.OutputFilePath = "";
            this.MachineName = "oGIMachine";
        }

        public void ProcessRequest()
        {
            string sXSLTFilePath = string.Format("{0}{1}.xslt", this._folderPath, this.LanguageCode);

            if (File.Exists(sXSLTFilePath))
            {
                // right now we only support javascript code generation...hence not checking on languagecode
                StreamReader sourceReader = new StreamReader(this.FilePath);
                string sCompleteSourceCode = sourceReader.ReadToEnd();
                sourceReader.Close();

                GIMachineGenerator.GIGenParser parser = new GIMachineGenerator.GIGenParser();
                GIMachineGenerator.GIMachine machine = parser.ParseGIGenSource(sCompleteSourceCode,this.MachineName);
                if (machine == null)
                {
                    Console.WriteLine("No machine generated... please check your code.");
                    return;
                }

                // Code generation is nothing but XSL transformation of the xml representation of machine by the
                // language specific XSL-T
                XmlDocument xd = new XmlDocument();
                // This will be replaced with xd.loadXml(...) call
                string sMachineXML = machine.ToXML();
                xd.LoadXml(sMachineXML);

                XslCompiledTransform xslt = new XslCompiledTransform();
                xslt.Load(sXSLTFilePath);
                StreamWriter stm = new StreamWriter( this.OutputFilePath );
                XsltArgumentList xsltArgs = new XsltArgumentList();
                xsltArgs.AddParam("MachineName", "", this.MachineName);
                xslt.Transform(xd, xsltArgs, stm);
                stm.Close();

                StreamReader resultReader = new StreamReader(this.OutputFilePath);
                Console.WriteLine(resultReader.ReadToEnd());
                resultReader.Close();
            }
            else
            {
                Console.WriteLine(string.Format("Error: No transformation XSLT found for language '{0}'", this.LanguageCode));
            }
        }

        //public void GenerateJavaScriptCodeForGIMachine(GIMachineGenerator.GIMachine machine, string machineName, StreamWriter oWriter)
        //{


        //}

        
    }
}
